﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Enterprisess;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Heightss
{
    public class ShowHeightHander : IRequestHandler<ShowHeightCommand, Result<PageResultDto<Heights>>>
    {


        private readonly IMapper mapper;

        private readonly IBaseRepository<Heights> heightsRepository;

        public ShowHeightHander(IMapper mapper, IBaseRepository<Heights> heightsRepository)
        {
            this.mapper = mapper;
            this.heightsRepository = heightsRepository;
        }



        public async Task<Result<PageResultDto<Heights>>> Handle(ShowHeightCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<Heights>>() { Code = (int)CommonErrorCode.Search };
            var page = new PageResultDto<Heights>() { List = new List<Heights>(), PageCount = 0, TotalCount = 0 };
            var res = await (await heightsRepository.GetAll()).ToListAsync();
            page.TotalCount = res.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.List = res.OrderBy(x => x.HeightId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = page;
            result.Code = (int)CommonErrorCode.Success;
            return result;
        }
    }
}
